package com.chaihu.generator.backend;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 */
public class BackCoderGeneratorStart extends BaseGenerator {


    protected BackCoderGeneratorStart() {
        // prevents calls from subclass
        throw new UnsupportedOperationException();
    }

    /**
     * 代码生成器 入口方法
     * 此处生成的mapper文件包含过程表和实例表的sql,所以要求两张表的特殊字段也要写上
     * BusinessTypeCd
     *
     * @param args 参数
     */
    public static void main(String[] args) throws Exception {

        //加载配置
        StringBuffer sb = readFile("backend/generator_info.json");

        JSONObject dataJson = JSONObject.parseObject(sb.toString());

        Data data = new Data();
        if(dataJson.containsKey("autoMove")){
            data.setAutoMove(dataJson.getBoolean("autoMove"));
        }
        data.setId(dataJson.getString("id"));
        data.setName(dataJson.getString("name"));
        data.setDesc(dataJson.getString("desc"));
        data.setModuleName(dataJson.getString("moduleName"));
        data.setTableName(dataJson.getString("tableName"));
        data.setParams(dataJson.getJSONObject("param"));

        JSONArray required = dataJson.getJSONArray("required");

        List<String> paramList = new ArrayList<String>();
        for (int reqIndex = 0; reqIndex < required.size(); reqIndex++) {
            JSONObject require = required.getJSONObject(reqIndex);
            paramList.add(require.getString("code"));
        }
        data.setRequiredParam(paramList.toArray(new String[required.size()]));

        //dto.txt
        GeneratorDtoBean generatorDtoBean = new GeneratorDtoBean();
        generatorDtoBean.generator(data);

        //IServiceDao.txt
        GeneratorIServiceDao generatorIServiceDao = new GeneratorIServiceDao();
        generatorIServiceDao.generator(data);

        //ServiceDaoMapper.txt
        GeneratorServiceDaoMapper generatorServiceDaoMapper = new GeneratorServiceDaoMapper();
        generatorServiceDaoMapper.generator(data);

        //ServiceCodeConstant.txt
        GeneratorServiceCodeConstant generatorServiceCodeConstant = new GeneratorServiceCodeConstant();
        generatorServiceCodeConstant.generator(data);

        //DeleteIn.txt DeleteListener.txt DeleteService.txt DeleteServiceImpl.txt
        GeneratorDeleteIn generatorDeleteIn = new GeneratorDeleteIn();
        generatorDeleteIn.generator(data);
        GeneratorDeleteListener generatorDeleteListener = new GeneratorDeleteListener();
        generatorDeleteListener.generator(data);
        GeneratorDeleteService generatorDeleteService = new GeneratorDeleteService();
        generatorDeleteService.generator(data);
        GeneratorDeleteServiceImpl generatorDeleteServiceImpl = new GeneratorDeleteServiceImpl();
        generatorDeleteServiceImpl.generator(data);

        //ListIn.txt ListListener.txt ListOut.txt ListService.txt ListServiceImpl.txt
        GeneratorListIn generatorListIn = new GeneratorListIn();
        generatorListIn.generator(data);
        GeneratorListListener generatorListListener = new GeneratorListListener();
        generatorListListener.generator(data);
        GeneratorListOut generatorListOut = new GeneratorListOut();
        generatorListOut.generator(data);
        GeneratorListService generatorListService = new GeneratorListService();
        generatorListService.generator(data);
        GeneratorListServiceImpl generatorListServiceImpl = new GeneratorListServiceImpl();
        generatorListServiceImpl.generator(data);

        //SaveIn.txt SaveListener.txt SaveService.txt SaveServiceImpl.txt
        GeneratorSaveIn generatorSaveIn = new GeneratorSaveIn();
        generatorSaveIn.generator(data);
        GeneratorSaveListener generatorSaveListener = new GeneratorSaveListener();
        generatorSaveListener.generator(data);
        GeneratorSaveService generatorSaveService = new GeneratorSaveService();
        generatorSaveService.generator(data);
        GeneratorSaveServiceImpl generatorSaveServiceImpl = new GeneratorSaveServiceImpl();
        generatorSaveServiceImpl.generator(data);

        //UpdateIn.txt UpdateListener.txt UpdateService.txt UpdateServiceImpl.txt
        GeneratorUpdateIn generatorUpdateIn = new GeneratorUpdateIn();
        generatorUpdateIn.generator(data);
        GeneratorUpdateListener generatorUpdateListener = new GeneratorUpdateListener();
        generatorUpdateListener.generator(data);
        GeneratorUpdateService generatorUpdateService = new GeneratorUpdateService();
        generatorUpdateService.generator(data);
        GeneratorUpdateServiceImpl generatorUpdateServiceImpl = new GeneratorUpdateServiceImpl();
        generatorUpdateServiceImpl.generator(data);

    }
}
